import {
  createRouter,
  createWebHistory,
  type RouteLocationNormalizedLoaded,
  type RouteRecordRaw
} from 'vue-router'
import {
  ApplicationEffect,
  ApplicationOne,
  DatabaseCode,
  DataScreen,
  MouseZodiac,
  Toolkit
} from '@icon-park/vue-next'

export const OVERVIEW: string = '概览'
export const OVERVIEW_PATH: string = '/overview'
//----------
export const DATA_FLOW: string = '数据流程'
export const DATA_FLOW_PATH: string = '/data-flow'
export const DATA_FLOW_LIST_PATH: string = '/data-flow/list'
export const DATA_FLOW_DESIGN_PATH: string = '/data-flow/design'
//----------
export const FLOW_INSTANCE: string = '流程实例'
export const FLOW_INSTANCE_PATH: string = '/flow-instance'
//----------
export const EXECUTION_ENGINE: string = '执行引擎'
export const EXECUTION_ENGINE_PATH: string = '/execution-engine'
//----------
export const PLATFORM_RESOURCE: string = '平台资源'
export const PLATFORM_RESOURCE_PATH: string = '/platform-resource'
export const PLATFORM_RESOURCE_OPERATOR_PACKAGE: string = '算子包'
export const PLATFORM_RESOURCE_OPERATOR_PACKAGE_PATH: string = '/platform-resource/operator-package'
export const PLATFORM_RESOURCE_DATA_SOURCE: string = '数据源'
export const PLATFORM_RESOURCE_DATA_SOURCE_PATH: string = '/platform-resource/data-source'
//----------
export const DEV_TOOL: string = '开发工具'
export const DEV_TOOL_PATH: string = '/dev-tool'
export const DEV_TOOL_FUNCTION_DESIGN: string = '算子设计'
export const DEV_TOOL_FUNCTION_DESIGN_PATH: string = '/dev-tool/function-design'
export const DEV_TOOL_FUNCTION_DESIGN_PROJECT_LIST_PATH: string =
  '/dev-tool/function-design/project-list'
export const DEV_TOOL_FUNCTION_DESIGN_FUNCTION_LIST_PATH: string =
  '/dev-tool/function-design/function-list'
export const DEV_TOOL_FUNCTION_DESIGN_FUNCTION_EDIT_PATH: string =
  '/dev-tool/function-design/function-edit'
export const DEV_TOOL_SDK: string = 'SDK'
export const DEV_TOOL_SDK_PATH: string = '/dev-tool/sdk'

export const breadcrumbConfig: Map<
  string,
  (currentRoute: RouteLocationNormalizedLoaded) => Array<{
    name: string
    path?: string
  }>
> = new Map([
  [
    OVERVIEW_PATH,
    (currentRoute: RouteLocationNormalizedLoaded) => {
      console.debug(currentRoute.name)
      return [{ name: OVERVIEW }]
    }
  ],
  [
    DATA_FLOW_LIST_PATH,
    () => {
      return [{ name: DATA_FLOW }]
    }
  ],
  [
    DATA_FLOW_DESIGN_PATH,
    (currentRoute: RouteLocationNormalizedLoaded): Array<{ name: string; path?: string }> => {
      const name = currentRoute.query.name as string
      return [{ name: DATA_FLOW, path: DATA_FLOW_PATH }, { name: name }]
    }
  ],
  [
    FLOW_INSTANCE_PATH,
    () => {
      return [{ name: FLOW_INSTANCE }]
    }
  ],
  [
    EXECUTION_ENGINE_PATH,
    () => {
      return [{ name: EXECUTION_ENGINE }]
    }
  ],
  [
    PLATFORM_RESOURCE_OPERATOR_PACKAGE_PATH,
    () => {
      return [{ name: PLATFORM_RESOURCE }, { name: PLATFORM_RESOURCE_OPERATOR_PACKAGE }]
    }
  ],
  [
    PLATFORM_RESOURCE_DATA_SOURCE_PATH,
    () => {
      return [{ name: PLATFORM_RESOURCE }, { name: PLATFORM_RESOURCE_DATA_SOURCE }]
    }
  ],
  [
    DEV_TOOL_FUNCTION_DESIGN_PROJECT_LIST_PATH,
    () => {
      return [{ name: DEV_TOOL }, { name: DEV_TOOL_FUNCTION_DESIGN }]
    }
  ],
  [
    DEV_TOOL_FUNCTION_DESIGN_FUNCTION_LIST_PATH,
    (currentRoute: RouteLocationNormalizedLoaded) => {
      const name = (currentRoute.query.name as string) || '新建项目-' + new Date().getTime()
      return [
        { name: DEV_TOOL },
        { name: DEV_TOOL_FUNCTION_DESIGN, path: DEV_TOOL_FUNCTION_DESIGN_PROJECT_LIST_PATH },
        { name: name }
      ]
    }
  ],
  [
    DEV_TOOL_FUNCTION_DESIGN_FUNCTION_EDIT_PATH,
    (currentRoute: RouteLocationNormalizedLoaded) => {
      const projectName =
        (currentRoute.query.projectName as string) || '新建项目-' + new Date().getTime()
      const functionName =
        (currentRoute.query.functionName as string) || '新建算子-' + new Date().getTime()
      return [
        { name: DEV_TOOL },
        {
          name: DEV_TOOL_FUNCTION_DESIGN,
          path: DEV_TOOL_FUNCTION_DESIGN_PROJECT_LIST_PATH
        },
        {
          name: projectName,
          path: DEV_TOOL_FUNCTION_DESIGN_FUNCTION_LIST_PATH
        },
        {
          name: functionName
        }
      ]
    }
  ],
  [
    DEV_TOOL_SDK_PATH,
    () => {
      return [{ name: DEV_TOOL }, { name: DEV_TOOL_SDK }]
    }
  ]
])

export const menuRoutes: Array<RouteRecordRaw> = [
  {
    path: OVERVIEW_PATH,
    name: 'overview',
    component: () => import('@/views/main/overview/Index.vue'),
    meta: {
      title: OVERVIEW,
      icon: DataScreen
    }
  },
  {
    path: DATA_FLOW_PATH,
    name: 'data-flow',
    meta: {
      title: DATA_FLOW,
      icon: ApplicationOne,
      sub: false
    },
    redirect: DATA_FLOW_LIST_PATH,
    children: [
      {
        path: DATA_FLOW_LIST_PATH,
        name: 'data-flow-list',
        component: () => import('@/views/main/data-flow/list/Index.vue'),
        meta: {
          belongMenuPath: DATA_FLOW_PATH
        }
      },
      {
        path: DATA_FLOW_DESIGN_PATH,
        name: 'data-flow-design',
        component: () => import('@/views/main/data-flow/design/Index.vue'),
        meta: {
          belongMenuPath: DATA_FLOW_PATH
        }
      }
    ]
  },
  {
    path: FLOW_INSTANCE_PATH,
    name: 'flow-instance',
    component: () => import('@/views/main/flow-instance/Index.vue'),
    meta: {
      title: FLOW_INSTANCE,
      icon: ApplicationEffect
    }
  },
  {
    path: EXECUTION_ENGINE_PATH,
    name: 'execution-engine',
    component: () => import('@/views/main/execution-engine/Index.vue'),
    meta: {
      title: EXECUTION_ENGINE,
      icon: MouseZodiac
    }
  },
  {
    path: PLATFORM_RESOURCE_PATH,
    name: 'platform-resource',
    meta: {
      title: PLATFORM_RESOURCE,
      icon: DatabaseCode
    },
    children: [
      {
        path: PLATFORM_RESOURCE_OPERATOR_PACKAGE_PATH,
        name: 'operator-package',
        component: () => import('@/views/main/platform-resource/operator-package/Index.vue'),
        meta: {
          title: PLATFORM_RESOURCE_OPERATOR_PACKAGE
        }
      },
      {
        path: PLATFORM_RESOURCE_DATA_SOURCE_PATH,
        name: 'data-source',
        component: () => import('@/views/main/platform-resource/data-source/Index.vue'),
        meta: {
          title: PLATFORM_RESOURCE_DATA_SOURCE
        }
      }
    ]
  },
  {
    path: DEV_TOOL_PATH,
    name: 'dev-tool',
    redirect: '/dev-tool/function-parameter-design',
    meta: {
      title: DEV_TOOL,
      icon: Toolkit
    },
    children: [
      {
        path: DEV_TOOL_FUNCTION_DESIGN_PATH,
        name: 'function-design',
        component: () => import('@/views/main/dev-tool/function-design/Index.vue'),
        meta: {
          title: DEV_TOOL_FUNCTION_DESIGN
        },
        redirect: DEV_TOOL_FUNCTION_DESIGN_PROJECT_LIST_PATH,
        children: [
          {
            path: DEV_TOOL_FUNCTION_DESIGN_PROJECT_LIST_PATH,
            name: 'function-design-project-list',
            component: () => import('@/views/main/dev-tool/function-design/project-list/Index.vue'),
            meta: {
              belongMenuPath: DEV_TOOL_FUNCTION_DESIGN_PATH
            }
          },
          {
            path: DEV_TOOL_FUNCTION_DESIGN_FUNCTION_LIST_PATH,
            name: 'function-design-function-list',
            component: () =>
              import('@/views/main/dev-tool/function-design/function-list/Index.vue'),
            meta: {
              belongMenuPath: DEV_TOOL_FUNCTION_DESIGN_PATH
            }
          },
          {
            path: DEV_TOOL_FUNCTION_DESIGN_FUNCTION_EDIT_PATH,
            name: 'function-design-function-edit',
            component: () =>
              import('@/views/main/dev-tool/function-design/function-edit/Index.vue'),
            meta: {
              belongMenuPath: DEV_TOOL_FUNCTION_DESIGN_PATH
            }
          }
        ]
      },
      {
        path: DEV_TOOL_SDK_PATH,
        name: 'sdk',
        component: () => import('@/views/main/dev-tool/sdk/Index.vue'),
        meta: {
          title: DEV_TOOL_SDK
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('@/views/login/Index.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('@/views/main/Index.vue'),
      redirect: '/overview',
      children: menuRoutes
    }
  ]
})

export default router
